package com.example.combwyaq.visualinterface.mainfragment;


import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.LogUtils;
import com.bumptech.glide.Glide;
import com.example.combwyaq.R;
import com.example.combwyaq.db.DaoManager;
import com.example.combwyaq.entity.UserEntity;
import com.example.combwyaq.mvp.contract.UserContract;
import com.example.combwyaq.mvp.di.DaggerUserComponent;
import com.example.combwyaq.mvp.di.UserModules;
import com.example.combwyaq.mvp.model.HttpCode;
import com.example.combwyaq.mvp.model.entity.BaseEntity;
import com.example.combwyaq.mvp.model.entity.UserItemEntity;
import com.example.combwyaq.mvp.model.entity.UserMenuEntity;
import com.example.combwyaq.mvp.presenter.UserPresenter;
import com.example.combwyaq.mvp.viwe.BaseFragment;
import com.example.combwyaq.utils.GlideUtils;
import com.example.combwyaq.visualinterface.activity.HomePageActivity;
import com.example.combwyaq.visualinterface.activity.LogActivity;
import com.example.combwyaq.visualinterface.adapter.UserAdapter;
import com.example.combwyaq.visualinterface.adapter.UserMenuAdapter;

import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class MainFragment_Mine extends BaseFragment<UserPresenter> implements UserContract.IUserView {

    private View menuView, itmeLineView;
    private RecyclerView rvMine;
    private UserAdapter adapter;
    private UserMenuAdapter menuAdapter;
    private RecyclerView menu_rv;
    private ImageView menuHeadImg;
    private TextView logReg;
    private LinearLayout homepage;
    private TextView menuUser;
    private RecyclerView menuRv;
    MyBroadcast myBroadcast = new MyBroadcast();
    private IntentFilter intentFilter = new IntentFilter();
    private TextView levelMine;
    private static final String TAG = "MainFragment_Mine";


    @Override
    public void refresh(BaseEntity entity) {
            LogUtils.json(entity);
    }

    @Override
    public void refreshMenu(List<UserMenuEntity> menuEntities) {
        menuAdapter.setNewData(menuEntities);


    }

    @Override
    public void refreshItemes(List<UserItemEntity> itemEntities) {
        adapter.setNewData(itemEntities);
    }

    @Override
    public int bindLayout() {
        return R.layout.fragment_main_fragment__mine;
    }

    @Override
    public void initView() {
        intentFilter.addAction("returnUser");
        getActivity().registerReceiver(myBroadcast, intentFilter);
        rvMine = f(R.id.rv_mine);
        menuView = LayoutInflater.from(getContext()).inflate(R.layout.view_user_menu, null);
        itmeLineView = LayoutInflater.from(getContext()).inflate(R.layout.view_user_speed, null);
        levelMine = (TextView) menuView.findViewById(R.id.level_mine);
        menuHeadImg = menuView.findViewById(R.id.menu_head_img);
        logReg = menuView.findViewById(R.id.log_reg);
        homepage = menuView.findViewById(R.id.homepage);
        menuUser = menuView.findViewById(R.id.menu_user);
        menu_rv = menuView.findViewById(R.id.menu_rv);
        try {
            List<UserEntity> userEntities = DaoManager.getInstance().getDaoSession().loadAll(UserEntity.class);
            logReg.setText(userEntities.get(0).user_name );
            levelMine.setVisibility(View.VISIBLE);
            levelMine.setText("Lv" + userEntities.get(0).user_integral);

        } catch (Exception e) {
            e.printStackTrace();
        }

        logReg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (logReg.getText().toString().equals("登陆/注册")) {
                    startPage(LogActivity.class);
                }else{
                    startPage(HomePageActivity.class);
                }
            }
        });

        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 4);
        gridLayoutManager.setOrientation(GridLayoutManager.VERTICAL);
        menu_rv.setLayoutManager(gridLayoutManager);
        menuAdapter = new UserMenuAdapter();
        menu_rv.setAdapter(menuAdapter);
        LinearLayoutManager manager = new LinearLayoutManager(getContext());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        rvMine.setLayoutManager(manager);
        adapter = new UserAdapter();
//        添加头布局
        adapter.addHeaderView(menuView, 0);
        adapter.addHeaderView(itmeLineView, 1);
        rvMine.setAdapter(adapter);
        rvMine.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL));



    }

    @Override
    public void inject() {
        DaggerUserComponent.builder()
                .userModules(new UserModules(this))
                .build().inject(this);

    }

    @Override
    public void initData() {
        p.initData();
        try {
            List<UserEntity> list = DaoManager.getInstance().getDaoSession().queryBuilder(UserEntity.class).list();
            if (list!=null){
                if (list.size()>0){
                    UserEntity entity=list.get(0);
                    refreshUser(entity);
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    @Override
    public void onStart() {
        super.onStart();
        try {
            List<UserEntity> userEntities = DaoManager.getInstance().getDaoSession().loadAll(UserEntity.class);
            LogUtils.json(userEntities);
            if (userEntities.get(0).getUser_img()==null){

                Glide.with(this).load(R.drawable.pn_placeholder).into(menuHeadImg);

            }else{
                Glide.with(this)
                        .load(userEntities.get(0).getUser_img())
                        .into(menuHeadImg);
            }
            logReg.setText(userEntities.get(0).user_name);


        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private void refreshUser(UserEntity entity) {
        logReg.setText(entity.user_name );
        levelMine.setVisibility(View.VISIBLE);
        levelMine.setText("Lv" + entity.user_integral);
        String s = entity.getUser_img().toString();
        Log.i(TAG, "refreshUser: "+s);
        p.selUser(entity.user_id, HttpCode.SELUSERCODE);


    }

    class MyBroadcast extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals("returnUser")) {
                UserEntity userEntity = intent.getParcelableExtra("userValues");
                refreshUser(userEntity);

            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
